﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SetSailModel;
using Dapper;

namespace SetSailDal
{
    /// <summary>
    /// 登录、注册
    /// </summary>
    public static class LoginDal
    {
        /// <summary>
        /// 查询字典
        /// </summary>
        /// <returns></returns>
        public static ResultModel<List<Cookie_ZD>> GetCookieInfo()
        {
            ResultModel<List<Cookie_ZD>> result = new ResultModel<List<Cookie_ZD>>();
            try
            {
                string sql = string.Format(@"SELECT a.*, b.MXDM, b.MXMC, b.MXZ 
                                             FROM QH_ZDGL a WITH(NOLOCK)
                                             INNER JOIN QH_ZDMX b WITH(NOLOCK) ON a.ID=b.ZDID");
                using (DBHelper help = new DBHelper())
                {
                    result.Data = help.DbContext.Query<Cookie_ZD>(sql) as List<Cookie_ZD>;
                }
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.Fail;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 注册申请
        /// </summary>
        /// <param name="zcsh"></param>
        /// <returns></returns>
        public static ResultModel<int> ADD_ZCSH(QH_ZCSH zcsh)
        {
            ResultModel<int> result = new ResultModel<int>();
            try
            {
                string sql_YHXX = string.Format(@"SELECT * FROM QH_YHXX a WITH(NOLOCK)
                                                 WHERE a.SCBJ=0 AND a.DLZH='{0}'",zcsh.ZCZH);
                string sql_ZCSH = string.Format(@"SELECT * FROM QH_ZCSH a WITH(NOLOCK)
                                                 WHERE a.SCBJ=0 AND a.SHZT='待审核' AND a.ZCZH='{0}'", zcsh.ZCZH);
                string sql = string.Format(@"INSERT INTO QH_ZCSH
                                             (
                                             	ID,
                                             	ZCZH,
                                             	ZCMM,
                                             	DDLX,
                                             	SQSJ,
                                             	SHZT,
                                             	SCBJ
                                             )
                                             VALUES
                                             (
                                             	@ID,
                                             	@ZCZH,
                                             	@ZCMM,
                                             	@DDLX,
                                             	GETDATE(),
                                             	@SHZT,
                                             	0
                                             )");
                using (DBHelper help = new DBHelper())
                {
                    var exist = help.DbContext.QueryFirstOrDefault(sql_ZCSH);
                    if (exist != null)
                    {
                        result.Code = ResultCode.Fail;
                        result.Message = "请勿重复申请注册，请联系管理员审核";
                    }
                    else
                    {
                        var exist2 = help.DbContext.QueryFirstOrDefault(sql_YHXX);
                        if (exist2 != null)
                        {
                            result.Code = ResultCode.Fail;
                            result.Message = "该账号已存在，请重新注册";
                        }
                        result.Count = help.DbContext.Execute(sql, zcsh);
                        if (result.Count == 0)
                        {
                            result.Code = ResultCode.Fail;
                            result.Message = "申请失败，请联系管理员";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.Fail;
                result.Message = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public static ResultModel<QH_YHXX> Login(string accont, string passward)
        {
            ResultModel<QH_YHXX> result = new ResultModel<QH_YHXX>();
            try
            {
                string sql = string.Format(@"SELECT TOP 1 * 
                                             FROM QH_YHXX a WITH(NOLOCK)
                                             WHERE a.SCBJ=0 AND a.DLZH='{0}' AND a.DLMM='{1}'",
                                             accont, passward);
                using (DBHelper help = new DBHelper())
                {
                    var data = help.DbContext.QueryFirstOrDefault<QH_YHXX>(sql);
                    if (data != null)
                    {
                        result.Data = data;
                    }
                    else
                    {
                        result.Code = ResultCode.Fail;
                        result.Message = "用户名或密码不正确";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.Fail;
                result.Message = ex.Message;
            }
            return result;
        }
    }
}
